//
//  ActionPicker.m
//  iDemo
//
//  Created by danal on 11-11-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ActionPicker.h"


@implementation ActionPicker
//@synthesize pickerDataSource,pickerDelegate;
@synthesize delegate;

- (id)initWithTitle:(NSString *)title delegate:(id<ActionPickerDelegate>)_delegate picker:(UIPickerView *)picker{

    if ((self = [super init])) {
        
        self.backgroundColor = [UIColor clearColor];
        
        self.delegate = _delegate;
        _as = [[UIActionSheet alloc] init];
        _as.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
        _as.delegate = self;
//        _as.title = title;
        
        _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, kToolBarHeight)];
        _toolbar.barStyle = UIBarStyleBlackTranslucent;
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, kToolBarHeight)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = title;
        
        UIBarButtonItem *bbtTitle = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
        
        UIBarButtonItem *bbtSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        UIBarButtonItem *bbtOK = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(OK)];
        bbtOK.width = 60.f;
        UIBarButtonItem *bbtCancel = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(Cancel)];
        bbtCancel.width = 60.f;
        
        _toolbar.items = [NSArray arrayWithObjects:bbtCancel,bbtSpace,bbtTitle,bbtSpace,bbtOK, nil];
    
        [_as addSubview:_toolbar];
 
        
        CGRect rect = CGRectMake(0, kToolBarHeight, 320, 216.0f);
        picker.frame = rect;

        [_as addSubview:picker];
        
        [self addSubview:_as];
        
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

#pragma mark - Actions

- (void)setBarStyle:(UIBarStyle)barStyle{
    _toolbar.barStyle = barStyle;
}


- (IBAction)showInView:(UIView *)_view{
    [_as showInView:_view];
    _as.bounds = CGRectMake(0, 0, 320, kActionSheetHeight);
}

- (IBAction)OK{
    [_as dismissWithClickedButtonIndex:0 animated:YES];

    if (delegate && [delegate respondsToSelector:@selector(didOKButtonPressed:)]) {
        [delegate didOKButtonPressed:self];
    }
}

- (IBAction)Cancel{
    [_as dismissWithClickedButtonIndex:0 animated:YES];  
    if (delegate && [delegate respondsToSelector:@selector(didCancelButtonPressed:)]) {
        [delegate didCancelButtonPressed:self];
    }
}


@end
